function [decoded_bits, decoded_symbols] = decode_symbol(received_signal, modulation_type)
% decode_symbol - 对接收信号进行解调和解码
%
% 输入参数:
%   received_signal  - 接收信号
%   modulation_type  - 调制类型: 'BPSK', 'QPSK', 'QAM16', 'QAM64'
%
% 输出参数:
%   decoded_bits     - 解码后的比特流
%   decoded_symbols  - 解码后的符号（在星座图上的最近点）

% 获取符号数量
num_symbols = length(received_signal);

% 根据调制类型获取每个符号对应的比特数
bits_per_symbol = get_bits_per_symbol(modulation_type);

% 初始化解码结果
decoded_symbols = zeros(1, num_symbols);
decoded_bits = zeros(1, num_symbols * bits_per_symbol);

% 根据不同调制类型进行解码
switch upper(modulation_type)
    case 'BPSK'
        % BPSK解调: 实部>0 -> 1, 实部<0 -> 0
        decoded_bits = real(received_signal) > 0;
        decoded_symbols = 2 * decoded_bits - 1;
        
    case 'QPSK'
        % QPSK解调
        % 定义QPSK星座点
        constellation = [(-1-1i)/sqrt(2), (-1+1i)/sqrt(2), (1-1i)/sqrt(2), (1+1i)/sqrt(2)];
        bit_mapping = [0 0; 0 1; 1 0; 1 1];
        
        for i = 1:num_symbols
            % 找到最近的星座点
            [~, idx] = min(abs(received_signal(i) - constellation));
            decoded_symbols(i) = constellation(idx);
            
            % 获取对应的比特
            bits = bit_mapping(idx, :);
            decoded_bits((i-1)*2+1:i*2) = bits;
        end
        
    case 'QAM16'
        % 16QAM解调
        % 创建16QAM星座图
        constellation = zeros(16, 1);
        bit_mapping = zeros(16, 4);
        
        idx = 1;
        for i1 = [0 1]
            for i2 = [0 1]
                for i3 = [0 1]
                    for i4 = [0 1]
                        bit_seq = [i1, i2, i3, i4];
                        bit_mapping(idx, :) = bit_seq;
                        
                        % 确定实部和虚部
                        real_part = 2 * bit_seq(1) - 1;
                        if bit_seq(2) == 0
                            real_part = real_part * 3;
                        end
                        
                        imag_part = 2 * bit_seq(3) - 1;
                        if bit_seq(4) == 0
                            imag_part = imag_part * 3;
                        end
                        
                        constellation(idx) = (real_part + 1i*imag_part) / sqrt(10);
                        idx = idx + 1;
                    end
                end
            end
        end
        
        for i = 1:num_symbols
            % 找到最近的星座点
            [~, idx] = min(abs(received_signal(i) - constellation));
            decoded_symbols(i) = constellation(idx);
            
            % 获取对应的比特
            bits = bit_mapping(idx, :);
            decoded_bits((i-1)*4+1:i*4) = bits;
        end
        
    case 'QAM64'
        % 64QAM解调（简化版）
        % 对于64QAM，我们使用硬判决直接映射到最近的星座点
        
        % 反归一化接收信号
        scaled_signal = received_signal * sqrt(42);
        
        for i = 1:num_symbols
            % 提取实部和虚部
            I = real(scaled_signal(i));
            Q = imag(scaled_signal(i));
            
            % 硬判决 - 将实部和虚部量化到[-7, -5, -3, -1, 1, 3, 5, 7]
            I_levels = [-7, -5, -3, -1, 1, 3, 5, 7];
            Q_levels = [-7, -5, -3, -1, 1, 3, 5, 7];
            
            [~, I_idx] = min(abs(I - I_levels));
            [~, Q_idx] = min(abs(Q - Q_levels));
            
            % 存储解码后的符号
            decoded_symbols(i) = (I_levels(I_idx) + 1i*Q_levels(Q_idx)) / sqrt(42);
            
            % 将索引转换为比特
            I_bits = de2bi(I_idx-1, 3, 'left-msb');
            Q_bits = de2bi(Q_idx-1, 3, 'left-msb');
            
            % 存储解码后的比特
            decoded_bits((i-1)*6+1:(i-1)*6+3) = I_bits;
            decoded_bits((i-1)*6+4:i*6) = Q_bits;
        end
        
    otherwise
        error('不支持的调制类型: %s', modulation_type);
end

end

function bits_per_symbol = get_bits_per_symbol(modulation_type)
% 内部辅助函数，根据调制方式获取每个符号承载的比特数

switch upper(modulation_type)
    case 'BPSK'
        bits_per_symbol = 1;
    case 'QPSK'
        bits_per_symbol = 2;
    case 'QAM16'
        bits_per_symbol = 4;
    case 'QAM64'
        bits_per_symbol = 6;
    otherwise
        error('不支持的调制类型: %s', modulation_type);
end
end 